home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / sysb091a.zip / sysbench / src / pmb_memspeed.c < prev    next >
C/C++ Source or Header  |  1996-05-19  |  6KB  |  324 lines

  1.  
  2. // memspeed test
  3.  
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <os2.h>
  7.  
  8. #include "types.h"
  9.  
  10. #define MB (1024*1024)
  11. #define KB (1024)
  12.  
  13. #define MIN_TIME 10.0 // minimal test time in seconds
  14.  
  15. extern double dtime(void);
  16. extern void err(char* s);
  17.  
  18. _Inline void touchf(long* p1, long n);
  19. _Inline void touchb(long* p1, long n);
  20. _Inline long readmem(long* p1, long n);
  21. _Inline void writemem(long* p1, long n);
  22. _Inline int memcpy2(long* p1, long* p2, long n);
  23. volatile long bluttanbla;
  24.  
  25. double
  26. pmb_memspeed(int bytes) {
  27.   char* p;
  28.   char* p1;
  29.   char* p2;
  30.   ULONG i,j, runs;
  31.   double t2, t1, tot_time, coerce1, coerce2;
  32.   ULONG mem2;
  33.   double value, tot_copied;
  34.  
  35.   mem2 = bytes/2;
  36.   p = (char*)malloc(bytes);
  37.   if (!p) {
  38.     err("memspeed test: can't allocate enough memory to do test");
  39.   }
  40.  
  41.   /* First, touch all the memory to activate it */
  42.   touchf((long*)p, bytes);
  43.   touchb((long*)(p+bytes), bytes);
  44.   touchf((long*)p, bytes);
  45.   touchb((long*)(p+bytes), bytes);
  46.  
  47.   runs = 1;
  48.   p1 = p;
  49.   p2 = p+mem2;
  50.   while (1) {
  51.     t1 = dtime();
  52.     for (i = 0; i < runs; i++) {
  53.       memcpy(p2, p1, mem2); // copy forwards
  54.       memcpy(p1, p2, mem2); // copy backwards
  55.     }
  56.     t2 = dtime();
  57.     tot_time = (t2-t1);
  58.     if ((tot_time) < MIN_TIME) {
  59.       if ((tot_time) < 0.1) {
  60.         runs *= MIN_TIME*10;
  61.       } else {
  62.         runs = (MIN_TIME*1.2)/(tot_time)*runs;
  63.       }
  64.     } else {
  65.       bluttanbla = p[(rand() * rand()) % bytes]; // fool optimizer
  66.       free(p);
  67.       coerce1 = (double)runs;
  68.       coerce2 = (double)bytes;
  69.       tot_copied = coerce1*coerce2;
  70.       value = (tot_copied/tot_time);
  71.       return value;
  72.     }
  73.   }
  74.   // we won't get here
  75.   err("memspeed: internal error 1");
  76.   return -1.0;
  77. }
  78.  
  79. double
  80. pmb_memspeedr(int bytes) {
  81.  
  82.   char* p;
  83.   ULONG i,j, runs;
  84.   double t2, t1, tot_time, tot_copied, coerce1, coerce2, value;
  85.  
  86.  
  87.   p = (char*)malloc(bytes);
  88.   if (!p) {
  89.     err("memspeed test: can't allocate enough memory to do test");
  90.   }
  91.  
  92.   /* First, touch all the memory to activate it */
  93.   touchf((long*)p, bytes);
  94.   touchb((long*)(p+bytes), bytes);
  95.  
  96.   runs = 1;
  97.   while (1) {
  98.     t1 = dtime();
  99.     for (i = 0; i < runs; i++) {
  100.       bluttanbla += readmem((long*)p, bytes);
  101.     }
  102.     t2 = dtime();
  103.     tot_time = (t2-t1);
  104.     if ((tot_time) < MIN_TIME) {
  105.       if ((tot_time) < 0.1) {
  106.         runs *= MIN_TIME*10;
  107.       } else {
  108.         runs = (MIN_TIME*1.2)/(tot_time)*runs;
  109.       }
  110.     } else {
  111.       bluttanbla += p[(rand() * rand()) % bytes]; // fool optimizer
  112.       free(p);
  113.       coerce1 = (double)runs;
  114.       coerce2 = (double)bytes;
  115.       tot_copied = coerce1*coerce2;
  116.       value = tot_copied/tot_time;
  117.       return value;
  118.     }
  119.   }
  120.   // we won't get here
  121.   err("memspeed: internal error 1");
  122.   return -1.0;
  123. }
  124.  
  125. double
  126. pmb_memspeedw(int bytes) {
  127.  
  128.   char* p;
  129.   ULONG i,j, runs;
  130.   double t2, t1, tot_time, tot_copied, coerce1, coerce2, value;
  131.   p = (char*)malloc(bytes);
  132.   if (!p) {
  133.     err("memspeed test: can't allocate enough memory to do test");
  134.   }
  135.  
  136.   /* First, touch all the memory to activate it */
  137.   touchf((long*)p, bytes);
  138.   touchb((long*)(p+bytes), bytes);
  139.  
  140.   runs = 1;
  141.   while (1) {
  142.     t1 = dtime();
  143.     for (i = 0; i < runs; i++) {
  144.       writemem((long*)p, bytes);
  145.     }
  146.     t2 = dtime();
  147.     tot_time = (t2-t1);
  148.     if ((tot_time) < MIN_TIME) {
  149.       if ((tot_time) < 0.1) {
  150.         runs *= MIN_TIME*10;
  151.       } else {
  152.         runs = (MIN_TIME*1.2)/(tot_time)*runs;
  153.       }
  154.     } else {
  155.       bluttanbla = p[(rand() * rand()) % bytes]; // fool optimizer
  156.       free(p);
  157.       coerce1 = (double)runs;
  158.       coerce2 = (double)bytes;
  159.       tot_copied = coerce1*coerce2;
  160.       value = tot_copied/tot_time;
  161.       return value;
  162.     }
  163.   }
  164.   // we won't get here
  165.   err("memspeed: internal error 1");
  166.   return -1.0;
  167. }
  168.  
  169. // not used:
  170. _Inline int
  171. memcpy2(long* p1, long* p2, long n) {
  172.   int i;
  173.   n = n >> 6;
  174.   for (i = 0; i < n; i++) {
  175.     *p1++ = *p2++;
  176.     *p1++ = *p2++;
  177.     *p1++ = *p2++;
  178.     *p1++ = *p2++;
  179.     *p1++ = *p2++;
  180.     *p1++ = *p2++;
  181.     *p1++ = *p2++;
  182.     *p1++ = *p2++;
  183.  
  184.     *p1++ = *p2++;
  185.     *p1++ = *p2++;
  186.     *p1++ = *p2++;
  187.     *p1++ = *p2++;
  188.     *p1++ = *p2++;
  189.     *p1++ = *p2++;
  190.     *p1++ = *p2++;
  191.     *p1++ = *p2++;
  192. /*
  193.     *p1++ = *p2++;
  194.     *p1++ = *p2++;
  195.     *p1++ = *p2++;
  196.     *p1++ = *p2++;
  197.     *p1++ = *p2++;
  198.     *p1++ = *p2++;
  199.     *p1++ = *p2++;
  200.     *p1++ = *p2++;
  201.  
  202.     *p1++ = *p2++;
  203.     *p1++ = *p2++;
  204.     *p1++ = *p2++;
  205.     *p1++ = *p2++;
  206.     *p1++ = *p2++;
  207.     *p1++ = *p2++;
  208.     *p1++ = *p2++;
  209.     *p1++ = *p2++; */
  210.   }
  211. }
  212.  
  213. _Inline void
  214. touchf(long* p1, long n) {
  215.   int i;
  216.   int r;
  217.   n = n/4;
  218.   for (i = 0; i < n; i++) {
  219.     bluttanbla += *p1;
  220.     *p1++ = rand();
  221.   }
  222. }
  223.  
  224. _Inline void
  225. touchb(long* p1, long n) {
  226.   int i;
  227.   int r;
  228.   n = n/4;
  229.   for (i = 0; i < n; i++) {
  230.     *(--p1)= rand();
  231.     bluttanbla += *p1;
  232.   }
  233. }
  234.  
  235. _Inline void
  236. writemem(long* p1, long n) {
  237.   int i;
  238.   n = n >> 7;
  239.   for (i = 0; i < n; i++) {
  240.     *p1++ = 0;
  241.     *p1++ = 0;
  242.     *p1++ = 0;
  243.     *p1++ = 0;
  244.     *p1++ = 0;
  245.     *p1++ = 0;
  246.     *p1++ = 0;
  247.     *p1++ = 0;
  248.  
  249.     *p1++ = 0;
  250.     *p1++ = 0;
  251.     *p1++ = 0;
  252.     *p1++ = 0;
  253.     *p1++ = 0;
  254.     *p1++ = 0;
  255.     *p1++ = 0;
  256.     *p1++ = 0;
  257.  
  258.     *p1++ = 0;
  259.     *p1++ = 0;
  260.     *p1++ = 0;
  261.     *p1++ = 0;
  262.     *p1++ = 0;
  263.     *p1++ = 0;
  264.     *p1++ = 0;
  265.     *p1++ = 0;
  266.  
  267.     *p1++ = 0;
  268.     *p1++ = 0;
  269.     *p1++ = 0;
  270.     *p1++ = 0;
  271.     *p1++ = 0;
  272.     *p1++ = 0;
  273.     *p1++ = 0;
  274.     *p1++ = 0;
  275.   }
  276.  
  277. }
  278.  
  279. _Inline long
  280. readmem(long* p1, long n) {
  281.   int i;
  282.   long t = 0;
  283.   n = n >> 7;
  284.   for (i = 0; i < n; i++) {
  285.     t += (*p1++);
  286.     t += (*p1++);
  287.     t += (*p1++);
  288.     t += (*p1++);
  289.     t += (*p1++);
  290.     t += (*p1++);
  291.     t += (*p1++);
  292.     t += (*p1++);
  293.  
  294.     t += (*p1++);
  295.     t += (*p1++);
  296.     t += (*p1++);
  297.     t += (*p1++);
  298.     t += (*p1++);
  299.     t += (*p1++);
  300.     t += (*p1++);
  301.     t += (*p1++);
  302.  
  303.     t += (*p1++);
  304.     t += (*p1++);
  305.     t += (*p1++);
  306.     t += (*p1++);
  307.     t += (*p1++);
  308.     t += (*p1++);
  309.     t += (*p1++);
  310.     t += (*p1++);
  311.  
  312.     t += (*p1++);
  313.     t += (*p1++);
  314.     t += (*p1++);
  315.     t += (*p1++);
  316.     t += (*p1++);
  317.     t += (*p1++);
  318.     t += (*p1++);
  319.     t += (*p1++);
  320.   }
  321.   return t;
  322. }
  323.  
  324.